﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 随机生成金币
public class MapController : MonoBehaviour {
	public GameObject propsWall;

	public GameObject doorWall;

	public GameObject superWall;

	public GameObject wall;


	public GameObject coinEntity0;
	public GameObject coinEntity1;
	public GameObject coinEntity2;
	public GameObject coinEntity3;
	private GameObject[] coinEntitys;


	private List<Vector2> posList = new List<Vector2> ();

	private static List<Vector2> superWallPosList = new List<Vector2>();
	// private Dictionary<string, int> typeDic = new Dictionary<string, int>();
	//地图范围
	private int xitm;
	private int yitm;

	// 每个玩家对应的颜色
	public int[] coinColorNum;
	// 可以产生的颜色数组
	private List<int> canProduceColorList = new List<int> ();
	// 每个颜色金币存在的数量
	private int[] colorCoinExisitNum;

	// const里当前表项
	private int currentDropExcelIndex;
	// 是否在生成金币中
	private bool producing;
	
	private List<int> coinColorList = new List<int> ();


	void Start(){
		
	}

	//创建地图 初始化地图 count：地图中生成可销毁wall的数量
	public void initMap(int count ,int enemyCount, int x , int y ){
		//清空list数据
		superWallPosList.Clear();
		posList.Clear ();
		coinColorList.Clear();
		colorCoinExisitNum = new int[4]{0, 0, 0, 0};
		coinEntitys = new GameObject[4] { coinEntity0, coinEntity1, coinEntity2, coinEntity3 };

		currentDropExcelIndex = 0;
		producing = false;

		this.xitm = x;
		this.yitm = y;

		destoryMap ();
		// 生成场景

		// 生成所有可产生金币的坐标
		produceAllPosList ();
		
	}





	//  产生金币
	public void produceCoinsArray(int goThroughTime){
		if(producing || currentDropExcelIndex >= Const.dropMap.GetLength(0))
			return;

		int needThroughTime = (int)(Const.dropMap[currentDropExcelIndex,0]);
		if(goThroughTime < needThroughTime)
			return;
		
		StopCoroutine ("DropCoin");
        //开启计时器
		StartCoroutine ("DropCoin"); 
		producing = true;
	}

	IEnumerator DropCoin(){
		int currentDropCoinIndex = 0;
		int needDropNum = Random.Range ((int)(Const.dropMap[currentDropExcelIndex,1]) , (int)(Const.dropMap[currentDropExcelIndex,2]));
		float coinDropIntervalMin = Const.dropMap[currentDropExcelIndex,3];
		float coinDropIntervalMax = Const.dropMap[currentDropExcelIndex,4];
		//随机时间产生下一枚金币
		for (int i = 0; i < needDropNum; i++) 
		{
			produceCoin();
			float rangeNum = Random.Range (coinDropIntervalMin , coinDropIntervalMax);
			yield return new WaitForSeconds (rangeNum);
			currentDropCoinIndex ++;
			
		}
		currentDropExcelIndex++;
		producing = false;
	}

	// 随机产生具体颜色的coin
	private void produceCoin()
	{
		getCanProduceColor();
		// 随机产生一个颜色
		if(canProduceColorList.Count == 0)
			return;
		
		int arrayIndex = Random.Range (0 , canProduceColorList.Count);
		int colorNum = canProduceColorList[arrayIndex];
		colorCoinExisitNum[colorNum] += 1;
		int index = Random.Range (0 , posList.Count);
		Vector2 coinPos = new Vector2(posList[index].x + Const.offsetX, posList[index].y + Const.offsetY);
		GameObject curCoinObj = Instantiate (coinEntitys[colorNum] , coinPos , Quaternion.identity);
		curCoinObj.GetComponent<CoinAI>().initCoin(colorNum, Const.coinDropAniTime, Const.coinAddWidth, Const.coinAddHeight);
	}

	// 可以产生的颜色
	private void getCanProduceColor()
	{
		canProduceColorList.Clear();
		for(int i = 0; i < coinColorNum.Length; i++)
		{
			int curColor = coinColorNum[i];
			if (colorCoinExisitNum[curColor] < Const.maxCoinExist)
			{
				// 颜色少的增加触发概率
				for(int j = 0; j < Const.maxCoinExist - colorCoinExisitNum[curColor]; j++)
				{
					canProduceColorList.Add(curColor);
				}
			}
		}
	}

	// 创建所有可产生金币的坐标
	private void produceAllPosList()
	{
		int allRow = Const.CanvasWidthGrid * 2;
		int allCol = Const.CanvasHeightGrid * 2;
		int allNum = allRow * allCol;

		// 可掉落坐标
		int[] canDropArray=new int[allNum];
		for (int i = 0; i < allNum; i++)
		{
			canDropArray[i] = 1;
		}
	
		int beginX = -Const.CanvasWidthGrid;
		int beginY = -Const.CanvasHeightGrid;

		// 遍历所有障碍物 记录不要的位置
		foreach (Vector2 cantPos in GenerateLevel.obstacalsPos)
		{
			int cantArrayIndex = (int)cantPos.x - beginX + ((int)cantPos.y - beginY) * allRow;
			if (cantArrayIndex < canDropArray.Length && cantArrayIndex >=0)
			{
				canDropArray[cantArrayIndex] = 0;
			}
		}
		foreach (Vector2 cantPos in GenerateLevel.mojingPos)
		{
			int cantArrayIndex = (int)cantPos.x - beginX + ((int)cantPos.y - beginY) * allRow;
			if (cantArrayIndex < canDropArray.Length && cantArrayIndex >=0)
			{
				canDropArray[cantArrayIndex] = 0;
			}
		}
		foreach (Vector2 cantPos in GenerateLevel.portalPos)
		{
			int cantArrayIndex = (int)cantPos.x - beginX + ((int)cantPos.y - beginY) * allRow;
			if (cantArrayIndex < canDropArray.Length && cantArrayIndex >=0)
			{
				canDropArray[cantArrayIndex] = 0;
			}
		}
		List<Vector2> cantSupList = new List<Vector2>();
		cantSupList.Add(new Vector2(-1,-8));
		cantSupList.Add(new Vector2(2,-3));
		cantSupList.Add(new Vector2(2,-1));
		cantSupList.Add(new Vector2(2,0));
		cantSupList.Add(new Vector2(4,1));
		cantSupList.Add(new Vector2(-4,1));
		cantSupList.Add(new Vector2(0,-4));
		cantSupList.Add(new Vector2(-1,3));
		cantSupList.Add(new Vector2(-1,4));
		cantSupList.Add(new Vector2(-1,5));
		cantSupList.Add(new Vector2(0,3));
		cantSupList.Add(new Vector2(0,4));
		cantSupList.Add(new Vector2(0,5));
		cantSupList.Add(new Vector2(2,3));
		cantSupList.Add(new Vector2(-2,7));
		cantSupList.Add(new Vector2(14,8));
		foreach (Vector2 cantPos in cantSupList)
		{
			int cantArrayIndex = (int)cantPos.x - beginX + ((int)cantPos.y - beginY) * allRow;
			if (cantArrayIndex < canDropArray.Length && cantArrayIndex >=0)
			{
				canDropArray[cantArrayIndex] = 0;
			}
		}


		// 记录所有障碍物之外的位置
		for (int x = beginX; x < allRow + beginX; x++) {
			for (int y = beginY; y < allCol + beginY; y ++) {
				int arrayIndex = (x - beginX) + (y - beginY) * allRow;
				if (arrayIndex >= 0 && arrayIndex < canDropArray.Length && canDropArray[arrayIndex] == 1)
				{
					posList.Add (new Vector2(x * 0.6f , y * 0.6f));

					// GameObject curCoinObj = Instantiate (coinEntitys[0] , new Vector2(x * 0.6f + 0.3f, y * 0.6f + 0.3f) , Quaternion.identity);
					// curCoinObj.GetComponent<CoinAI>().initCoin(0, Const.coinDropAniTime, Const.coinAddWidth, Const.coinAddHeight);
				}
			}
		}
	}

    //销毁场景中的物体
    void destoryMap(){
		GameObject[] coins = GameObject.FindGameObjectsWithTag ("Coin");	

		foreach (GameObject w in coins) {
			Destroy (w);
		}
	}
}
